package com.jumi.microservice.material.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jumi.microservice.material.domain.JmMaterialSmallUnit;
import com.jumi.microservice.material.domain.example.JmMaterialSmallUnitExample;
import com.jumi.microservice.material.dto.JmUnitResponse;
import com.jumi.microservice.material.mapper.JmMaterialSmallUnitMapper;
import com.jumi.microservice.material.service.JmMaterialSmallUnitService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description: 商品最小单位service服务组件
 * @author: ZhangY
 */
@Service
@Transactional
public class JmMaterialSmallUnitServiceImpl implements JmMaterialSmallUnitService
{

	@Autowired
	private JmMaterialSmallUnitMapper jmMaterialSmallUnitMapper;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public PageInfo<JmMaterialSmallUnit> findAll(Integer page, Integer size, String smunitName, Integer smunitStatus, Date beginTime, Date endTime)
	{
		if (smunitName == null || smunitName.length() <= 0)
		{
			smunitName = null;
		}
		JmMaterialSmallUnitExample smunitExample = new JmMaterialSmallUnitExample();
		smunitExample.setOrderByClause("update_time DESC");
		JmMaterialSmallUnitExample.Criteria smunitExampleCriteria = smunitExample.createCriteria();

		smunitExampleCriteria.andIsDelEqualTo(0);
		if (StringUtils.isNotEmpty(smunitName)){
			smunitExampleCriteria.andSmunitNameLike("%" + smunitName + "%");
		}
		if (smunitStatus != null){
			smunitExampleCriteria.andSmunitStatusEqualTo(smunitStatus);
		}
		if(beginTime!=null){
			smunitExampleCriteria.andUpdateTimeGreaterThanOrEqualTo(beginTime);
		}
		if(endTime!=null){
			smunitExampleCriteria.andUpdateTimeLessThanOrEqualTo(endTime);
		}
		PageHelper.startPage(page, size);
		List<JmMaterialSmallUnit> smunits = jmMaterialSmallUnitMapper.selectByExample(smunitExample);
		return new PageInfo<>(smunits);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public JmMaterialSmallUnit findById(Integer smunitId)
	{
		return jmMaterialSmallUnitMapper.selectByPrimaryKey(smunitId);
	}

	@Override
	public int add(JmMaterialSmallUnit smunit)
	{
		//检查重名
		JmMaterialSmallUnitExample smunitExample = new JmMaterialSmallUnitExample();
		JmMaterialSmallUnitExample.Criteria smunitExampleCriteria = smunitExample.createCriteria();
		smunitExampleCriteria.andSmunitNameEqualTo(smunit.getSmunitName());
		List<JmMaterialSmallUnit> smunits = jmMaterialSmallUnitMapper.selectByExample(smunitExample);
		if (smunits.size() > 0)
		{
			//重名
			return 0;
		}
		jmMaterialSmallUnitMapper.insertSelective(smunit);
		return 1;
	}

	@Override
	public int update(JmMaterialSmallUnit smunit)
	{
		JmMaterialSmallUnitExample smunitExample = new JmMaterialSmallUnitExample();
		JmMaterialSmallUnitExample.Criteria smunitExampleCriteria = smunitExample.createCriteria();
		//检查重名
		smunitExampleCriteria.andSmunitNameEqualTo(smunit.getSmunitName());
		smunitExampleCriteria.andSmunitIdNotEqualTo(smunit.getSmunitId());
		List<JmMaterialSmallUnit> smunits = jmMaterialSmallUnitMapper.selectByExample(smunitExample);
		if (smunits.size() > 0)
		{
			//重名
			return 0;
		}
		jmMaterialSmallUnitMapper.updateByPrimaryKeySelective(smunit);
		return 1;
	}

	@Override
	public int deleteByIds(String[] ids, String updateBy)
	{
		for (String id : ids)
		{
			//启动状态不能删除
			JmMaterialSmallUnitExample smunitExample = new JmMaterialSmallUnitExample();
			JmMaterialSmallUnitExample.Criteria smunitExampleCriteria = smunitExample.createCriteria();
			smunitExampleCriteria.andSmunitStatusEqualTo(1);
			smunitExampleCriteria.andSmunitIdEqualTo(Integer.valueOf(id));
			List<JmMaterialSmallUnit> smunits = jmMaterialSmallUnitMapper.selectByExample(smunitExample);
			if (smunits.size() > 0)
			{
				//启动状态不能删除
				return 0;
			}
		}
		for (String id : ids)
		{
			jmMaterialSmallUnitMapper.deleteByPrimaryKey(Integer.valueOf(id), updateBy);
		}
		return 1;
	}

	@Override
	public int setStatus(Integer smunitId, Integer smunitStatus, String updateBy)
	{
		JmMaterialSmallUnit smunit = new JmMaterialSmallUnit();
		smunit.setSmunitId(smunitId);
		smunit.setSmunitStatus(smunitStatus);
		smunit.setUpdateBy(updateBy);
		return jmMaterialSmallUnitMapper.updateByPrimaryKeySelective(smunit);
	}

	@Override
	public List<JmUnitResponse> findByUnitIds(List<Integer> unitIds)
	{
		return jmMaterialSmallUnitMapper.findByUnitIds(unitIds);
	}
}
